package com.example.allshengmedical.mainmenu.activity;

import java.io.IOException;
import java.security.InvalidParameterException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

//import com.allsheng.serial.SerialOD600;
import com.allsheng.widget.AllShengDialog;
import com.allsheng.widget.AllShengDialog.ClickListenerInterface;
import com.example.allshengmedical.R;
import com.example.allshengmedical.base.BaseActivity;
import com.example.allshengmedical.base.BaseApplication;
import com.example.allshengmedical.experimentanalyze.activity.AnalyzeBrowseActivity;
import com.example.allshengmedical.experimentnew.activity.FileManagerActivity;
import com.example.allshengmedical.experimentnew.activity.ManagerActivity1;
import com.example.allshengmedical.experimentrun.activity.BrowseActivity;
import com.example.allshengmedical.mainmenu.presenter.MainMenuPresenter;
import com.example.allshengmedical.mainmenu.view.IMainMenuView;
import com.example.allshengmedical.system.activity.SystemActivity;
import com.example.allshengmedical.util.ByAlert;
import com.example.allshengmedical.util.SharedPreferencesUtils;
import com.example.allshengmedical.welcome.activity.WelcomeActivity;

//进入app的4个开始界面
public class MainMenuActivity extends BaseActivity implements IMainMenuView,
		OnClickListener {

	LinearLayout newLine, runLine, analyzeLine, systemLine;
	ImageView screenChageIv;

	MainMenuPresenter mainMenuPresenter;
	boolean isRun;
	SharedPreferences preferences;
//	TextView check_tv;

	@Override
	protected int setcontent() {
		return R.layout.activity_mainmenu;
	}

	@Override
	protected void initView() {
		preferences = getSharedPreferences("SelfCheckFail", MODE_PRIVATE);
		isRun = preferences.getBoolean("SelfCheckFail", true);
		mainMenuPresenter = new MainMenuPresenter(this);
		mainMenuPresenter.InitAppHead();
		Toast.makeText(MainMenuActivity.this, "2017.6.14 13：40", Toast.LENGTH_SHORT).show();

	}

	@Override
	protected void initData() {
	}

	@Override
	public void jumpInToNew() {
		Intent intent = new Intent(MainMenuActivity.this,
				ManagerActivity1.class);
		startActivity(intent);
		MainMenuActivity.this.finish();
	}

	@Override
	public void jumpInToRun() {
		if (isRun) {
			Intent intent = new Intent(MainMenuActivity.this,
					BrowseActivity.class);
			startActivity(intent);
		} else {
			final AllShengDialog allShengDialog = new AllShengDialog(this,
					"自检请求错误信息", preferences.getString("SelfCheckDetail", ""),
					"OK");

			allShengDialog.show();
			allShengDialog.setClicklistener(new ClickListenerInterface() {

				@Override
				public void OK() {
					// TODO Auto-generated method stub
					allShengDialog.dismiss();
				}

				@Override
				public void Cancle() {
					// TODO Auto-generated method stub
					allShengDialog.dismiss();
				}
			});
		}

		// finish();

	}

	@Override
	public void jumpInToAnalyze() {
		Intent intent = new Intent(MainMenuActivity.this,
				AnalyzeBrowseActivity.class);
		startActivity(intent);
		// finish();

	}

	@Override
	public void jumpInToSystem() {
		Intent intent = new Intent(MainMenuActivity.this, SystemActivity.class);
		startActivity(intent);
		// finish();
	}

	@Override
	public void initAppHead() {
		TextView date, fileName;
		date = BindView(R.id.date);
		fileName = BindView(R.id.file_name);
		// ��ȡϵͳʱ��
		SimpleDateFormat sdfDate = new SimpleDateFormat("MM dd,yyyy");
		date.setText(sdfDate.format(new Date()));

		fileName.setText("");
	}

	@Override
	public void screenChange() {
		// ByAlert.alert("�����л����ܣ�");
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.newLine:
//			freeMemory();
			mainMenuPresenter.JumpInToNew();
//			mainMenuPresenter = null;
			break;
		case R.id.runLine:
//			freeMemory();
			mainMenuPresenter.JumpInToRun();
//			mainMenuPresenter = null;
			break;
		case R.id.analyzeLine:
//			freeMemory();
			mainMenuPresenter.JumpInToAnalyze();
//			mainMenuPresenter = null;
			break;
		case R.id.systemLine:
//			freeMemory();
			mainMenuPresenter.JumpInToSystem();
//			mainMenuPresenter = null;
			break;
		case R.id.screenChageIv:
//			freeMemory();
			mainMenuPresenter.ScreenChange();
//			mainMenuPresenter = null;
			break;

		default:
			break;
		}
	}
	
	private void freeMemory()
	{
		newLine = null; runLine = null; analyzeLine = null; systemLine = null;

	}

}
